<?php
error_reporting(E_ALL);
ini_set('display_errors','On');


require '../../MyFrame/Component/Autoloader.php';
//require '../../MyFrame/Component/Kernel.php';
//require '../../MyFrame/Component/Controller.php';
require '../../MyFrame/Component/Template.php';
//require '../../MyFrame/Component/Router.php';
//require '../../MyFrame/Component/Database.php';
//require '../../MyFrame/Component/Request.php';

Autoloader::init();

class ControllerFactory
{
  static function create($controller, $action)
  {


    $controllerName = ucfirst($controller)."Controller";
    $actionName = "execute".ucfirst($action);

    #include("../controllers/".$controller."/$controllerName.php");

    $controllerObj = new $controllerName();


    $controllerObj->layoutName = 'default';
    $controllerObj->viewTemplateName = "{$controller}/{$action}";
    return $controllerObj;
  }
}
$app = new Kernel();
$app->setRequest(new Request());
$app->setRouter(new Router());

$app->router->parse($app->request->pathInfo, $app->request->method);
$app->request->mergeQuery($app->router->parsed);


$app->controllerName = $app->router->parsed['_controller'];
$app->actionName = $app->router->parsed['_action'];

$controllerName = ucfirst($app->controllerName)."Controller";
$actionName = "execute".ucfirst($app->actionName);


#include("../controllers/".$app->controllerName."/$controllerName.php");

$controller = ControllerFactory::create($app->controllerName, $app->actionName);

$controller->beforeAction();
$controller->$actionName();
$controller->afterAction();

$content = Template::render_template($controller->viewTemplateName, $controller->viewVars);
include("../layout/{$controller->layoutName}.php");

?>
